import MatteAssembler from "./matte_assembler";

const { executeInEditMode, ccclass, property } = cc._decorator;

@ccclass
@executeInEditMode
export default class Matte extends cc.RenderComponent {
    _rc: cc.RenderComponent = null;
    get rc() {
        if (!this._rc) {
            let rc = this.getComponent(cc.RenderComponent);
            if (!rc) {
                rc = this.addComponent(cc.RenderComponent);
            }
            if (!rc) {
                cc.error("ShaderBase::rc, error: 不能获得或添加cc.RenderComponent组件。");
                return;
            }
            this._rc = rc;
        }
        return this._rc;
    }

    _material: cc.Material = null;
    get material() {
        return this.rc.getMaterial(0);
    }
    protected onLoad(): void {
        this.refresh();
    }

    @property({ serializable: true })
    _matte = 1;
    @property
    get matte() {
        return this._matte;
    }
    set matte(v) {
        this._matte = v;
        this.refresh();
    }

    @property({ serializable: true })
    _minVal = 1;
    @property
    get minVal() {
        return this._minVal;
    }
    set minVal(v) {
        this._minVal = v;
        this.refresh();
    }

    @property({ serializable: true })
    _maxVal = 1;
    @property
    get maxVal() {
        return this._maxVal;
    }
    set maxVal(v) {
        this._maxVal = v;
        this.refresh();
    }

    refresh() {
        this.material.setProperty("matte", this._matte);
        this.material.setProperty("min_clamp", this._minVal);
        this.material.setProperty("max_clamp", this._maxVal);
    }
    onColorChanged() {
        this.updateAssembler();
    }

    override _assembler: MatteAssembler = null;
    //override
    _resetAssembler() {
        this.setVertsDirty(true);
        this._assembler = new MatteAssembler();
        this._assembler.init(this);
    }
    onEnable() {
        super.onEnable();
        this._activateMaterial();
        this.node.on(cc.Node.EventType.POSITION_CHANGED, this.updateAssembler, this);
        this.node.on(cc.Node.EventType.SIZE_CHANGED, this.updateAssembler, this);
        this.node.on(cc.Node.EventType.ANCHOR_CHANGED, this.updateAssembler, this);
        this.node.on(cc.Node.EventType.COLOR_CHANGED, this.onColorChanged, this);

        this.onColorChanged();
    }
    onDisable() {
        this.node.off(cc.Node.EventType.POSITION_CHANGED, this.updateAssembler, this);
        this.node.off(cc.Node.EventType.SIZE_CHANGED, this.updateAssembler, this);
        this.node.off(cc.Node.EventType.ANCHOR_CHANGED, this.updateAssembler, this);
        this.node.off(cc.Node.EventType.COLOR_CHANGED, this.onColorChanged, this);
    }

    updateAssembler() {
        this.setVertsDirty(true);
    }

    protected lateUpdate(dt: number): void {
        // if (CC_EDITOR) {
        //     this.matte += dt * aniSpeed;
        //     if (this.matte >= 1.0) {
        //         this.matte = 1.0;
        //         aniSpeed *= -1;
        //     }
        //     else if (this.matte <= 0.0) {
        //         this.matte = 0.0;
        //         aniSpeed *= -1;
        //     }
        // }
    }
};

// var aniSpeed = 0.5;